﻿using System;
using System.Linq;
using BankCreditPortfolio.DAL.Entities;

namespace BankCreditPortfolio.DAL.Services
{
    public class PaymentService : DataService<Payment>
    {
        public IQueryable<Payment> GetAllByDepositId(int depositId)
        {
            var filteredQuery = this.CreateQuery().Where(x => x.Deposit.DepositId == depositId);
            return filteredQuery.Count() > 0 ? filteredQuery : null;
        }

        public IQueryable<Payment> GetAllByStatus(string status)
        {
            var filteredQuery = this.CreateQuery().Where(x => x.PaymentStatus.Title == status);
            return filteredQuery.Count() > 0 ? filteredQuery : null;
        }

        public decimal GetAmountByPeriod(string status, DateTime start, DateTime stop)
        {
            var filteredQuery =
                this.CreateQuery().Where(x => x.PaymentStatus.PaymentStatusId == 2 && x.Date >= start.Date && x.Date < stop.Date);
            decimal summ = 0;
            if (filteredQuery.Count() > 0)
            {
                foreach (var agreement in filteredQuery)
                {
                    switch (agreement.Currency.Title)
                    {
                        case "BYR":
                            summ += agreement.Amount / 8700;
                            break;
                        case "EURO":
                            summ += agreement.Amount * (decimal)1.56;
                            break;
                        case "RUR":
                            summ += agreement.Amount / 30;
                            break;
                        default:
                            summ += agreement.Amount;
                            break;
                    }
                }
            }
            return summ;
        }
    }
}
